<?php
namespace Service\Controller;
use Think\Controller;
class IndexController extends Controller {
	public function index(){}
	public function tida_cart_result(){
		//加购完成的主动通知API
		$data["app_key"] = $_GET["app_key"];
		$data["method"] = $_GET["method"];
		$data["timestamp"] = $_GET["timestamp"];
		$data["itemId"] = $_GET["itemId"];
		$data["skuId"] = $_GET["skuId"];
		$data["sellerNick"] = $_GET["sellerNick"];
		$data["mixBuyerNick"] = $_GET["mixBuyerNick"];
		$data["isvExt"] = $_GET["isvExt"];
		$data["traceId"] = $_GET["traceId"];
		$data["m"] = "service";
		$data["c"] = "index";
		$data["a"] = "tida_cart_result";
		
		$sign = $_GET["sign"];
		$check = $this->generateSign($data);
		
		if($sign==$check){
			$code = explode("_",$data["isvExt"]);
			if($code[0]=="cartChanceShopmobile")
			{ //用于抽奖活动验证加购成功的业务模块
				$ITEM = M("SmEventsUsersCartItems");
				// $s["sid"]=0;
				// $s["user_id"]=0;
				// $s["eid"]=0;
				// $s["score"]=0;
				$s["item_id"]=$data["itemId"];
				$s["sku_id"]=$data["skuId"];
				$s["seller_nick"]=$data["sellerNick"];
				$s["mix_buyer_nick"]=$data["mixBuyerNick"];
				$s["trace_id"]=$data["traceId"];
				$s["isv_ext"]=$data["isvExt"]; //不重复数据
				$s["add_time"]=date("Y-m-d H:i:s");
				$ITEM->add($s);
			}
			header("HTTP/1.1 200 OK ");   
			header("Content-type:text/xml");   
			echo "<recieved>true</recieved>";
		}else{
			header("HTTP/1.0 400 Bad Request");  
			header("Content-type:text/xml"); 
			echo "<recieved>false</recieved>"; 
		}
		
	}
	
	protected function generateSign($params)
	{
		ksort($params);

		$stringToBeSigned = C("FT_SECRET");
		foreach ($params as $k => $v)
		{
			if("@" != substr($v, 0, 1))
			{
				$stringToBeSigned .= "$k$v";
			}
		}
		unset($k, $v);
		$stringToBeSigned .= C("FT_SECRET");
		return strtoupper(md5($stringToBeSigned));
	}

}